package com.qzboot.backend.web.module.sys.controller;

import com.qzboot.backend.web.common.config.SysLog;
import com.qzboot.backend.web.module.sys.BaseSysController;
import com.qzboot.common.constant.SysConst;
import com.qzboot.common.support.WebResult;
import com.qzboot.facade.entity.SysRole;
import com.qzboot.facade.service.SysRoleService;
import com.qzboot.facade.service.SysUserRoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author zengzhihong
 * @date 2019-12-16
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseSysController {

    @Reference
    private SysRoleService sysRoleService;
    @Reference
    private SysUserRoleService sysUserRoleService;

    /**
     * 查询所有角色
     *
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:role:list")
    public WebResult list() {
        if (SysConst.ADMIN_USER_ID.equals(getUserId())) {
            return WebResult.success(sysRoleService.list());
        }
        return WebResult.success(sysRoleService.listByCreateBy(getUserId()));
    }

    /**
     * 通过用户ID查询角色
     *
     * @return
     */
    @GetMapping("/listRoleIdByUserId")
    @RequiresPermissions("sys:role:list")
    public WebResult listRoleIdByUserId(Long userId) {
        return WebResult.success(sysUserRoleService.listRoleIdByUserId(userId));
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @GetMapping("/listPage")
    @RequiresPermissions("sys:role:list")
    public WebResult listPage(@RequestParam Map<String, Object> param) {
        //非管理员只能查询自己创建的角色
        if (!SysConst.ADMIN_USER_ID.equals(getUserId())) {
            param.put("create_by_user_id", getUserId());
        }
        return WebResult.success(sysRoleService.page(param));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @SysLog("新增角色")
    @PostMapping("/save")
    @RequiresPermissions("sys:role:save")
    public WebResult save(@Valid @RequestBody SysRole entity) {
        entity.setCreateByUserId(getUserId());
        entity.setCreateByUsername(getUser().getUsername());
        entity.setUpdateByUserId(getUserId());
        entity.setUpdateByUsername(getUser().getUsername());
        sysRoleService.save(entity);
        return WebResult.success();
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @SysLog("修改角色")
    @PostMapping("/updateById")
    @RequiresPermissions("sys:role:update")
    public WebResult updateById(@Valid @RequestBody SysRole entity) {
        entity.setCreateByUserId(getUserId());
        entity.setCreateByUsername(getUser().getUsername());
        entity.setUpdateByUserId(getUserId());
        entity.setUpdateByUsername(getUser().getUsername());
        sysRoleService.updateById(entity);
        return WebResult.success();
    }

    /**
     * 通过ID删除
     *
     * @param id
     * @return
     */
    @SysLog("删除角色")
    @GetMapping("/removeById")
    @RequiresPermissions("sys:role:delete")
    public WebResult removeById(Long id) {
        sysRoleService.removeById(id, getUserId());
        return WebResult.success();
    }

    @SysLog("分配角色菜单")
    @PostMapping("/allocateRoleMenu")
    @RequiresPermissions("sys:role:update")
    public WebResult allocateRoleMenu(@RequestBody SysRole entity) {
        sysRoleService.allocateRoleMenu(entity.getId(), entity.getMenuIdList(), getUserId());
        return WebResult.success();
    }

}
